package com.school.test.search.function.interceptor;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.school.test.search.constants.RequestConst;
import com.school.test.search.function.base.controller.BaseTeaAndStuController;
import com.school.test.search.utils.MyStrUtil;

import java.util.Arrays;
import java.util.Collection;

/**
 * @author 马洁
 * @date 2020/1/3 - 17:13
 */
public class NotNullInterceptor implements Interceptor {

    @Override
    public void intercept(Invocation inv) {
        Controller controller = inv.getController();

        if (RequestConst.POST.equals(controller.getRequest().getMethod())
                && controller instanceof BaseTeaAndStuController) {

            BaseTeaAndStuController c = (BaseTeaAndStuController) controller;
            Collection<String[]> params = c.getParaMap().values();


            if (params
                    .stream()
                    .anyMatch(values -> {
                        if (!Arrays.stream(values)
                                .allMatch(MyStrUtil::isNotBracketStr)) {
                            c.toSearch();
                            return false;
                        }
                        return true;
                    })) {
                inv.invoke();
            }
        } else {
            inv.invoke();
        }
    }
}
